package com.longxi.tianjintianbao.ui.presenter.patrol;

import android.content.Intent;
import android.view.View;

import com.longxi.tianjintianbao.AppConst;
import com.longxi.tianjintianbao.greedao.controller.DataPatrolController;
import com.longxi.tianjintianbao.greedao.entity.Patrol;
import com.longxi.tianjintianbao.greedao.entity.PatrolContent;
import com.longxi.tianjintianbao.model.completeEnter.EnterEditText;
import com.longxi.tianjintianbao.model.completeEnter.EnterState;
import com.longxi.tianjintianbao.model.completeEnter.PicturesPath;
import com.longxi.tianjintianbao.ui.activity.common.CompleteEnterActivity;
import com.longxi.tianjintianbao.ui.adapter.multitype.LabelViewBinder;
import com.longxi.tianjintianbao.ui.adapter.multitype.patrol.PatrolContentViewBinder;
import com.longxi.tianjintianbao.ui.adapter.multitype.PatrolDetailViewBinder;
import com.longxi.tianjintianbao.ui.base.BaseActivity;
import com.longxi.tianjintianbao.ui.base.BasePresenter;
import com.longxi.tianjintianbao.ui.presenter.common.CompleteEnterAtPresenter;
import com.longxi.tianjintianbao.ui.view.patrol.IPatrolDetailAtView;
import com.longxi.tianjintianbao.utils.MyUtils;
import com.longxi.tianjintianbao.utils.TimeComparisonUtils;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import me.drakeet.multitype.Items;
import me.drakeet.multitype.MultiTypeAdapter;

public class PatrolDetailAtPresenter extends BasePresenter<IPatrolDetailAtView> {
    private long billId = 0;
    private boolean isScan = false;
    private Patrol patrol = null;
    private List<PicturesPath> selectList = new ArrayList<>();

    private MultiTypeAdapter adapter;
    private Items items;

    public PatrolDetailAtPresenter(BaseActivity context) {
        super(context);
    }

    public void initAdapter(){
        if(adapter == null){
            adapter = new MultiTypeAdapter();
            /* 注册类型和 View 的对应关系 */
            adapter.register(Patrol.class, new PatrolDetailViewBinder());
            adapter.register(String.class, new LabelViewBinder());
            adapter.register(PatrolContent.class, new PatrolContentViewBinder());
            getView().getRvContent().setAdapter(adapter);
        }
    }

    public void setDate() {
        Intent intent = mContext.getIntent();
        billId = intent.getLongExtra(AppConst.BILLID, 0);
        isScan = intent.getBooleanExtra(AppConst.ISSCAN, false);
        if(billId == 0){
            MyUtils.showToast("数据异常");
            mContext.showNoData();
            return;
        }

        patrol = DataPatrolController.getPatrolByBillid(billId);

        if(patrol == null){
            MyUtils.showToast("数据异常");
            mContext.showNoData();
            return;
        }

//        List<PatrolContent> patrolContentList = new ArrayList<>();
//        for(int i = 0; i < 3; i++){
//            PatrolContent patrolContent = new PatrolContent();
//            patrolContent.setItem("设备" + i);
//            patrolContentList.add(patrolContent);
//        }
//        List<TaskProgress> taskProgressList = new ArrayList<>();
//        for(int i = 0; i < 7; i++){
//            TaskProgress taskProgresses = new TaskProgress();
//            taskProgresses.setBilldate("2018-5-10");
//            taskProgresses.setPerson("操作者");
//            taskProgresses.setContent("操作内容");
//            taskProgressList.add(taskProgresses);
//        }
        items = new Items();
        items.add(patrol);
        items.add("请查看需巡查内容");
        items.addAll(patrol.getContent());
//        items.add(new ProgressLabel("巡检", "巡检中"));
//        items.addAll(taskProgressList);
        adapter.setItems(items);
        adapter.notifyDataSetChanged();
        mContext.hideLoading();

        showBtn();
    }

    private void showBtn() {
        if(!isScan){
            getView().getLlBtn().setText("只能通过扫一扫来操作");
            getView().getLlBtn().setVisibility(View.VISIBLE);
            getView().getLlBtn().setEnabled(false);
            return;
        }

        switch (mContext.getIntent().getStringExtra(AppConst.ISDRAFT)) {
            case "0":
                getView().getLlBtn().setText("完成该巡检");
                getView().getLlBtn().setVisibility(View.VISIBLE);
                break;
            case "1":
                getView().getLlBtn().setText("修改内容");
                getView().getLlBtn().setVisibility(View.VISIBLE);
                break;
            default:
                getView().getLlBtn().setVisibility(View.GONE);
                break;
        }
    }

    public void toCompleteEnterActivity() {
        if(TimeComparisonUtils.getNowDate(patrol.getStartdate(), patrol.getEnddate()) == TimeComparisonUtils.TIME_IS_NOT){
            MyUtils.showToast("巡检时间未到");
            return;
        }

        Intent intent = new Intent(mContext, CompleteEnterActivity.class);
        intent.putExtra(CompleteEnterAtPresenter.TITLE, "编辑巡查结果");

        EnterEditText enterEditText = new EnterEditText("请输入完成说明", patrol.getExplain());
        intent.putExtra(CompleteEnterAtPresenter.ENTER_EDITTEXT, enterEditText);

        List<EnterState> enterStateList = new ArrayList<>();
        enterStateList.add(new EnterState("正常", "1", true));
        enterStateList.add(new EnterState("异常", "0", false));
        if(patrol.getState() != null){
            for (EnterState enterState : enterStateList) {
                if(patrol.getState().equals(enterState.getStateValue())){
                    enterState.setSelect(true);
                }else{
                    enterState.setSelect(false);
                }
            }
        }
        intent.putExtra(CompleteEnterAtPresenter.ENTER_STATE_LIST, (Serializable) enterStateList);

        mContext.startActivityForResult(intent, AppConst.IntentRequstCode.ACTIVITY_COMPLETE_ENTER);
    }

    public void onActivityResult(int requestCode, Intent data) {
        switch (requestCode) {
            case AppConst.IntentRequstCode.ACTIVITY_COMPLETE_ENTER:
                if(data == null){
                    return;
                }
                patrol.setExplain(data.getStringExtra(CompleteEnterAtPresenter.ENTEREDITTEXT));//添加说明
                patrol.setState(data.getStringExtra(CompleteEnterAtPresenter.SELECTSTATE));//添加状态
                patrol.setIsDraft("1");//添加本地巡检状态

                if(TimeComparisonUtils.getNowDate(patrol.getStartdate(), patrol.getEnddate()) == 0){
                    patrol.setApplicant("0");//未完成提交
                }else{
                    patrol.setApplicant("2");//完成提交
                }

                selectList = (List<PicturesPath>) data.getSerializableExtra(CompleteEnterAtPresenter.SELECTPIC);
                for (int i = 0; i < selectList.size(); i++) {
                    if(i == 0){
                        patrol.setImage1(selectList.get(i).getPath());
                    }
                    if(i == 1){
                        patrol.setImage2(selectList.get(i).getPath());
                    }
                }

                patrol.setReleasetime(TimeComparisonUtils.getDate());//提交时间
                DataPatrolController.correctPatrol(patrol);//更新数据
                mContext.setResult(mContext.RESULT_OK);//告诉前一个页面要刷新数据了
                mContext.finish();//关闭当前页面

                break;
        }
    }
}
